/*
   Copyright 2009 Marcin Szczepanski, webqem (marcins@gmail.com)

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
 
package webqem.wave.api
{
	import flash.external.ExternalInterface;

	public class GadgetAPI implements IGadgetAPI
	{
		
		private var _isInWave:Boolean = false;
		private var _gadgetName:String;
		
		public function get isInWave():Boolean 
		{
			return _isInWave;
		}
		
		public function GadgetAPI()
		{
             _isInWave = ExternalInterface.call("waveIsInWave");		
		}
		
        public function initialiseAndSetCallbacks(
            gadgetName:String,
            waveStateCallback:Function, 
            waveParticipantCallback:Function,
            waveModeCallback:Function):void
	   {
			_gadgetName = gadgetName;
			
			ExternalInterface.addCallback(
                "waveStateCallback", 
                waveStateCallback);
			ExternalInterface.addCallback(
                "waveParticipantCallback", 
                waveParticipantCallback);
			ExternalInterface.addCallback(
                "waveModeCallback", 
                waveModeCallback);
			ExternalInterface.call("initialiseFlash", _gadgetName);
		}
		
		public function getState(key:String, defaultValue:String):Object
		{
			return ExternalInterface.call("waveStateGet", key, defaultValue);	
		}
		
		public function getStateKeys():Array
		{
			return ExternalInterface.call("waveStateGetKeys");
		}
		
		public function submitDelta(delta:Object):void
		{
			var keys:Array = new Array();
			var values:Array = new Array();
			
			/* it seems that just passing an Object to ExternalInterface.call
			* doesn't pass it as an assoc array, so instead we break up the
			* delta object into separate key and value arrays and pass those.
			* The bridge JS function handles the reassembly
			*/
			for(var key:String in delta) {
				keys.push(key);
				values.push(delta[key]);
			}
			
			ExternalInterface.call("waveStateSubmitDelta", keys, values);
		}		
	
		public function stateToString():String
		{
			return ExternalInterface.call("waveStateToString");
		}

		public function getViewer():String
		{
			return ExternalInterface.call("waveParticipantGetViewer");
		}
		
		public function getHost():String
		{
			return ExternalInterface.call("waveParticipantGetHost");
		}

		public function getAllParticipants():Array
		{
			return ExternalInterface.call("waveParticipantGetAll");
		}
		
		public function getParticipantDisplayName(id:String):String
		{
			return ExternalInterface.call("waveParticipantDisplayName", id);
		}

		public function getParticipantThumbnailUrl(id:String):String
		{
			return ExternalInterface.call("waveParticipantThumbnailUrl", id);
		}
		
		public function getWaveMode():String
		{
			return ExternalInterface.call("waveGetMode");
		}		
	}
}